﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Permissions;
using System.Security.Principal;
using System.Web.Security;
using System.Threading;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;

namespace HIS.Controllers.Home {

    public class Default : HIS.Controllers.AppBaseController {

        public Default() {
        }

        [Authorization(RoleAction = "all")]
        public override void Index() {
            try {
                Data.Add("Pagetitle", "Dashboard");
                RenderFile("default.html");
            } catch {
            }
        }

        [Authorization(RoleAction = "all")]
        public void Login() {

            try {
                if (HttpContext.Current.Request.Form.Count > 0) {
                    string strUserName = HttpContext.Current.Request["username"].NullSafeToString();
                    string strPassword = HttpContext.Current.Request["password"].NullSafeToString();

                    if (ValidateUserToLDAP(strUserName, strPassword)) {
                        
                        SysMembership sysMembership = null;
                        try {
                            string strUri = Global.ServiceUriDictionary["membershipuser"] + strUserName + Global.ServiceUriDictionary["requestformat"];
                            StringReader stringReader = RestService.GetJson(strUri);
                            sysMembership = JsonConvert.DeserializeObject<SysMembership>(stringReader.ReadToEnd());
                        } catch {
                        }
                        
                        
                        if (sysMembership != null) {

                            HttpCookie cookie = new HttpCookie("userticketdata", KeanuMvc.Reference.CryptoString.Encrypt(strUserName + "|" + sysMembership.RoleId));
                            HttpContext.Current.Response.Cookies.Add(cookie);
                            HttpContext.Current.Session["userticketdata"] = cookie.Value;
                            if (sysMembership.SysRole.DefaultUrl != null) {
                                HttpContext.Current.Response.Redirect(sysMembership.SysRole.DefaultUrl);
                            } else {
                                HttpContext.Current.Response.Redirect(Data["rooturl"].ToString());
                            }
                            
                        }
                    }
                }

                RenderFile("login.html");
            } catch { }
        }

        private bool ValidateUserToLDAP(string username, string password) {
            return true;
        }

        [Authorization(RoleAction = "all")]
        public void Logout() {
            try {
                HttpContext.Current.Session.Clear();
                HttpContext.Current.Session.Abandon();
                GoToUrl(Data["rooturl"].ToString() + "home/default/login/");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
            }
        }

        [Authorization(RoleAction = "all")]
        public void LogoutAcc() {
            try {
                HttpContext.Current.Session.Clear();
                HttpContext.Current.Session.Abandon();
                Data.Add("Pagetitle", "Session Expired");
            } catch {
            } finally {
                RenderFile("logoutacc.html");
            }
        }

        [Authorization(RoleAction = "all")]
        public void LogoutAccAjax() {
            HttpContext.Current.Response.Write("{\"error\":\"expired\"}");
        }

        [Authorization(RoleAction = "all")]
        public void LogoutTest() {
            try {
                HttpContext.Current.Session.Clear();
                HttpContext.Current.Session.Abandon();
            } catch (Exception ex) {
            } finally {
            }
        }

        [Authorization(RoleAction = "all")]
        public void Setup() {
            
        }

        [Authorization(RoleAction = "all")]
        private void ConfigurationSetup() {
            System.Configuration.ConfigurationManager.AppSettings.Add("apikey", "");
            System.Configuration.ConfigurationManager.AppSettings.Add("hospitalcode", "");
            System.Configuration.ConfigurationManager.AppSettings.Add("rooturl", "");
            
        }

        [Authorization(RoleAction = "all")]
        private void CacheDataSetup() {
           
        }

        [Authorization(RoleAction = "all")]
        public void UnAuthorize() {
            try {
                HttpContext.Current.Response.Write("Unauthorize!");
            } catch {
            }
        }

        [Authorization(RoleAction = "all")]
        public void CheckSession() {
            string IsSessionExist = "true";
            try {
                if (HttpContext.Current.Session["userticketdata"] != null) {
                    IsSessionExist = "true";
                }
            } catch  {
            } finally {
                HttpContext.Current.Response.Write(IsSessionExist);
            }
        }

        //private void SampleMedicalRecord() {
        //    Subjective subjective = new Subjective();
        //    Objective objective = new Objective();
        //    Allergies kk = new Allergies();
        //    objective.Allergies.Add(kk);

        //    Allergies gg = objective.Allergies.OrderByDescending(s => s.Id).First();

        //    string ss = JsonConvert.SerializeObject(objective);
        //}

    }


    

}